﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsFileComboBox.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2009
// </copyright>
// <summary>
//   Defines the SettingsFileComboBox type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SettingsManager
{
  #region Namespace Imports

  using System;
  using System.Collections.Generic;
  using System.Diagnostics.CodeAnalysis;
  using System.Windows.Forms;

  using JetBrains.Annotations;

  #endregion


  /// <summary>
  /// Describes and manipulates the settings file combo box.
  /// </summary>
  internal sealed partial class SettingsFileComboBox : ComboBox
  {
    #region Constants and Fields

    private ResharperSettingsManager _settingsManager;

    #endregion


    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="SettingsFileComboBox"/> class.
    /// </summary>
    public SettingsFileComboBox()
    {
      InitializeComponent();

      RefreshList();
    }

    #endregion


    #region Delegates

    private delegate void ListRefreshDelegate();

    #endregion


    #region Properties

    /// <summary>
    /// Gets or sets the selected file.
    /// </summary>
    /// <value>
    /// The selected item.
    /// </value>
    [CanBeNull]
    public SettingsFile SelectedFile
    {
      get
      {
        return SelectedItem as SettingsFile;
      }

      set
      {
        SelectedItem = value;
      }
    }

    /// <summary>
    /// Sets the selected file path.
    /// </summary>
    /// <value>
    /// The selected file path.
    /// </value>
    public string SelectedFilePath
    {
      set
      {
        if (_settingsManager == null)
        {
          return;
        }

        foreach (SettingsFile settingsFile in _settingsManager.SettingsFiles)
        {
          if (settingsFile.Path.FullPath == value)
          {
            SelectedFile = settingsFile;
          }
        }
      }
    }

    /// <summary>
    /// Sets the settings manager.
    /// </summary>
    /// <value>
    /// The settings manager.
    /// </value>
    public ResharperSettingsManager SettingsManager
    {
      set
      {
        if (_settingsManager != null)
        {
          _settingsManager.SettingsFileListChanged -= SettingsManagerSettingsFileListChanged;
        }

        _settingsManager = value;

        if (_settingsManager != null)
        {
          _settingsManager.SettingsFileListChanged += SettingsManagerSettingsFileListChanged;
        }

        RefreshList();
      }
    }

    #endregion


    #region Methods

    /// <summary>
    /// Refreshes the list.
    /// </summary>
    private void RefreshList()
    {
      SettingsFile previousSelectedFile = SelectedFile;

      Items.Clear();

      if (_settingsManager == null)
      {
        return;
      }

      IEnumerable<SettingsFile> settingsFiles = _settingsManager.SettingsFiles;
      SettingsFile selectedFile = null;

      foreach (SettingsFile file in settingsFiles)
      {
        if (previousSelectedFile != null && file.Path == previousSelectedFile.Path)
        {
          selectedFile = file;
        }

        Items.Add(file);
      }

      if (selectedFile != null)
      {
        SelectedFile = selectedFile;
      }
    }


    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", 
      Justification = "Any exceptions should be suppressed here.")]
    private void SettingsManagerSettingsFileListChanged([NotNull] object sender, [NotNull] EventArgs e)
    {
      try
      {
        ListRefreshDelegate refreshDelegate = RefreshList;
        Invoke(refreshDelegate);
      }
      catch
      {
        return;
      }
    }

    #endregion
  }
}